﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CustomControls_ActualizarProducto : System.Web.UI.UserControl
{
    protected int Id
    {
        get
        {
            if (ViewState["id"] == null) return 0;
            return (int)ViewState["id"];
        }
        set { ViewState["id"] = value; }
    }

    protected String[] Message
    {
        get
        {
            if (Session["Message"] == null)
            {
                String[] m = {"",""};
                return m;
            }
            return (String[])Session["Message"];
        }
        set { Session["Message"] = value; }
    }
    private bool Add
    {
        get
        {
            if (ViewState["add"] != null)
            {
                return (bool)ViewState["add"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["add"] = value;
        }
    }
    private bool Edit
    {
        get
        {
            if (ViewState["Edit"] != null)
            {
                return (bool)ViewState["Edit"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["Edit"] = value;
        }
    }
    private bool View
    {
        get
        {
            if (ViewState["View"] != null)
            {
                return (bool)ViewState["View"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["View"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            String[] msg = { "", "" };
            Message = msg;
            BLLProducts bll = new BLLProducts();

            if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.Url.ToString().Contains("ActualizarProducto.aspx"))
            {
                int cid = 0;
                int.TryParse(Request.QueryString["id"], out cid);
                this.Id = cid;
                Product p = bll.GetProductById(this.Id);
                if (p.Id > 0)
                {
                    this.lblCurrentQuantityValue.Text = ((p.Quantity != null) ? ((p.Unit == GlobalEnums.Units.kg.ToString()
                        || p.Unit == GlobalEnums.Units.l.ToString())
                        ? (p.Quantity / 1000).ToString() : p.Quantity.ToString()) : "0") + " " +p.Unit;
                    if (p.Unit != null)
                    {
                        if (p.Unit == GlobalEnums.Units.g.ToString() || p.Unit == GlobalEnums.Units.kg.ToString())
                        {
                            drpUnit.Items.Remove(new ListItem(GlobalEnums.Units.l.ToString(), GlobalEnums.Units.l.ToString()));
                            drpUnit.Items.Remove(new ListItem(GlobalEnums.Units.ml.ToString(), GlobalEnums.Units.ml.ToString()));
                        }
                        else
                        {
                            drpUnit.Items.Remove(new ListItem(GlobalEnums.Units.kg.ToString(), GlobalEnums.Units.l.ToString()));
                            drpUnit.Items.Remove(new ListItem(GlobalEnums.Units.g.ToString(), GlobalEnums.Units.g.ToString()));
                        }
                    }
                }
                else
                {
                    String[] error = { Resources.Resource.ERROR, Resources.Resource.PRODUCT_NOT_FOUND };
                    Message = error;
                    this.Response.Redirect("Default.aspx?recover=y");
                }
            }
            else
            {
                if ((Request.QueryString["id"] == null))
                {
                    String[] error = { Resources.Resource.ERROR, Resources.Resource.INVALID_URL };
                    Message = error;
                    this.Response.Redirect("Default.aspx?recover=y");
                }
                
            }

           SetupPageByUserRoles();

        }
        ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "0";
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {

        Product p = new Product();

        p.Quantity = double.Parse(txtQuantity.Text);
        p.Quantity = (drpUnit.SelectedValue == GlobalEnums.Units.kg.ToString() 
            || drpUnit.SelectedValue == GlobalEnums.Units.l.ToString())
                ? (Convert.ToDouble(txtQuantity.Text) * 1000) : Convert.ToDouble(txtQuantity.Text);
        p.Id = this.Id;
        p.Unit = drpUnit.SelectedValue;
        BLLProducts bll = new BLLProducts();
        bool flag = this.rdIncrease.Checked;
        if (bll.UpdateProductQuantity(p,flag))
        {
            String[] error = { Resources.Resource.SUCCESS, Resources.Resource.PRODUCT_EDITED };
            Message = error;
            this.Response.Redirect("Default.aspx?recover=y");
        }
        else
        {
            ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "1";
            ((Literal)Page.Master.FindControl("ltrMessage")).Text = Resources.Resource.UPDATE_ERROR;
            ((Literal)Page.Master.FindControl("ltrMessageType")).Text = Resources.Resource.ERROR;
        }
      
        
        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("Default.aspx?recover=y");
    }
    protected void SetupPageByUserRoles()
    {
        List<SessionPermissions> userPermissions = new List<SessionPermissions>();
        userPermissions = (List<SessionPermissions>)Session[GlobalNames.UserPermissions];
        userPermissions = (from upb in userPermissions
                           where upb.ModuleId == (long)GlobalEnums.Modules.Productos
                           select upb).ToList();
        if (userPermissions.Count() > 0)
        {
            foreach (SessionPermissions up in userPermissions)
            {
                if (up.PermissionId == (long)GlobalEnums.Permissions.Agregar)
                    Add = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Editar)
                    Edit = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    this.View = true;
            }
            if (!View)
                this.Response.Redirect("~/Default.aspx");
            
            if (Id > 0 && !Edit)
                btnSave.Visible = false;
        }
        else
        {
            this.Response.Redirect("~/Default.aspx");
        }
    }
    
    
}